﻿using AuthCenter.Domain;
using AuthCenter.ErorCode;
using AuthCenter.Infrastructure;
using AuthCenter_API.Write.Applications.Commands.RBAC;
using AutoMapper;
using MediatR;

namespace AuthCenter_API.Write.Applications.CommandHandlers.RBAC
{
    public class RoleDelCommandHandler : IRequestHandler<RoleDelCommand, APIResult<object>>
    {
        private readonly IBaseRepository<RoleModel> _roleRepository;
        private readonly IMapper mapper;
        public RoleDelCommandHandler(IBaseRepository<RoleModel> roleRepository, IMapper mapper)
        {
            _roleRepository = roleRepository;
            this.mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(RoleDelCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Code = APIEnum.DelSuccess;
            result.Message = APIEnum.DelSuccess.Desc();
            RoleModel role = mapper.Map<RoleModel>(request);
            role.IsDel = 1;
            await _roleRepository.Update(role);
            return await Task.FromResult(result);
        }
    }
}
